<?php
if(!defined('DCS_ROOT')) {exit('Access Denied');} 

class review {

	function __construct (){
		$this->url = getUrl();
		$action = $this->url['params']['1'];
		require_once(APP_PATH."model/admin/init.php" );
		if($action == 'add'){
			$this->add();
			exit();
		}elseif($action == 'edit'){
			$this->edit();
			exit();
		}elseif($action == 'del'){
			$this->del();
			exit();
		}else{
			$this->defaultAction();
			exit();
		}
	}
	function defaultAction(){
		$tpl = new Template( "admin/review/list.htm" );
		require_once(APP_PATH."model/admin/review.php");
		$db  = getDB();
		$data = new ReviewModel ($db);
		if($this->url['params']['1'] == 'page'){
			$pagetemp = $this->url['params']['2'];		
		}
		$param['table'] = ' movie_review AS r,movie AS m,member AS u ';
		$param['field'] = ' r.id,r.mid,r.uid,r.title,r.addtime,m.title mtitle,u.username ';
		$param['where'] = " r.uid= u.uid AND r.mid = m.id ";
		$param['order'] = ' r.addtime ';
		$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';		
		$param['rows'] = '10';
		$param['pageUrl'] = ADMIN.'review/page/';
		if(isPost()){
			$keyword = htmlClear(addslashes(trim($_POST['keyword'])));
			if(!empty($keyword)){
				$param['where'] .= " AND r.title LIKE '%$keyword%' ";
			}
		}
		$param['pageTotal']	= $db->getTotal($param);		
		$movieList = $db->getList($param);
		foreach($movieList as $k => $v ){
			$movieList[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
			$movieList[$k]['title'] = highlightWords($v['title'],$keyword);
		}
		$tpl->assign('movieList', $movieList);
		$tpl->assign("pageParam", makePage($param));
		$tpl->output();
	}

	function edit(){
		require_once(APP_PATH."model/admin/review.php" );
		$db  = getDB();
		$data = new ReviewModel( $db );
		$id = $this->url['params']['2'];
		if(!is_numeric($id)){
			message('找不到此条影评。');
		}
		$review = $data->getOne($id);		
		if(!isPost()){			
			$tpl = new Template("admin/review/edit.htm" );
			$review['addtime'] = date('Y-m-d H:i:s',$review['addtime']);			
			$review['contents'] = htmlspecialchars($review['contents']);
			$tpl->assign($review);
			$tpl->output();
		}else{
			$var['title'] = trim($_POST['title']);
			$var['addtime'] = trim($_POST['addtime']);		
			$var['contents'] = daddslashes($_POST['contents']);
			if(!empty($var['addtime'])){
				$var['addtime'] = strtotime($var['addtime']);
			}
			$edit = $data->editOne($id,$var['title'],$var['addtime'],$var['contents']);
			if ($edit){
				$message = '修改影评成功。';
			}else{
				$message = '修改影评失败！';
			}
			message($message);			
		}
	}


	function del(){
		require_once(APP_PATH."model/admin/review.php" );
		$db  = getDB();
		$data = new ReviewModel( $db );
		$id = $this->url['params']['2'];
		if(!is_numeric($id)){
			error('找不到此条影评信息。');
		}
		$del = $data->delOne($id);
		if ($del){
			$message = '删除影评成功。';
		}else{
			$message = '删除影评失败！';
		}
		exit($message);
	}

}
?>
